﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using FinPlusCommon;
using log = Logger.Logger;

namespace FinPlusWPFControls
{
    public partial class FinPlusTabPanel : FinPlusControl
    {
        private readonly IDictionary<string, FinPlusTabItem> _tabs;
        private enum Params { Tag, Id }
        private readonly string _user;

        public event EventHandler TabClosed;

        //construct
        public FinPlusTabPanel()
        {
            InitializeComponent();
            _user = Environment.UserName;
            _tabs = new Dictionary<string, FinPlusTabItem>();
        }

        //common control interface
        public override void CmdExecute(IFunction command)
        {
            try
            {
                CmdCapture(command);

                switch ((Cmds)Enum.Parse(typeof(Cmds), command.Name, true))
                {
                    case Cmds.Open: Open(command[Params.Id.ToString()].ToString(), command[Params.Tag.ToString()].ToString()); break;
                    case Cmds.Save: Save(command[Params.Id.ToString()].ToString()); break;
                    case Cmds.Home: Home(command[Params.Id.ToString()].ToString()); break;
                    case Cmds.Close: Close(command[Params.Id.ToString()].ToString()); break;
                    case Cmds.Closed: Closed(command[Params.Id.ToString()].ToString()); break;
                    case Cmds.Select: Select(); break;
                    default: throw new Exception(string.Format("event not recognised {0}", command.Name));
                }
            }
            catch (CmdExecuteException e)
            {
                IsValid = log.Log(Logger.Level.Warning, Id, Config, e.Message, e);
            }
        }

        //private
        private void Open(string name, string path)
        {
            if (_tabs.ContainsKey(name)) return;

            var t = new FinPlusTabItem(path, name) {IsSelected = true, Style = (Style)FindResource("tabItemStyle")};
            t.CmdExecute(new Function(Cmds.Open.ToString(), Name, _user));
            t.TabClosed += Tab_OnClosed;

            tab.Items.Add(t);
            _tabs.Add(t.Id, t);
         }

        private void Save(string name)
        {
            if (_tabs.ContainsKey(name)) 
                _tabs[name].CmdExecute(new Function(Cmds.Save.ToString(), Name, _user));
        }

        private void Home(string name)
        {
            if (_tabs.ContainsKey(name)) 
                _tabs[name].IsSelected = true;
        }

        private void Close(string name)
        {
            if (_tabs.ContainsKey(name))
                _tabs[name].CmdExecute(new Function(Cmds.Close.ToString(), Name, _user));
        }

        private void Closed(string name)
        {
            if (!_tabs.ContainsKey(name)) return;

            tab.Items.Remove(_tabs[name]);
            _tabs.Remove(name);

            TabClosed.Invoke(name, new EventArgs());
        }

        private void Select()
        {
            foreach (FinPlusTabItem t in tab.Items)
                t.ShowButtons(t.IsSelected);
        }

        //cmds
        private enum Cmds { Open, Save, Home, Close, Closed, Select,  }

        private void Tab_OnClosed(object s, EventArgs a){ CmdRouter(Cmds.Closed.ToString(), s.ToString());}
        private void Tab_Select(object s, SelectionChangedEventArgs a){ CmdRouter(Cmds.Select.ToString());}
    }
}
